package com.yc.forum.servlet;

import com.yc.forum.dao.RedisDao;
import com.yc.forum.dao.ReplyDao;
import com.yc.forum.util.Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.io.IOException;
import java.util.*;

/**
 * 发布评论：
 *  1.将贴子的评论数加一
 *  2.存入评论信息
 *  3.为发布者增加1点积分
 */
@WebServlet(name = "PutOneReplyServlet", value = "/PutOneReplyServlet.s")
public class PutOneReplyServlet extends HttpServlet {
    ReplyDao replyDao=new ReplyDao();
    RedisDao redisDao=new RedisDao();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session= req.getSession();
        String account;
        try{
            account=session.getAttribute("loginedUser").toString();
            String postId=req.getParameter("postId");
            String body=req.getParameter("body");
            int a= replyDao.putOneReply(postId,account,body);
            //将帖子的评论数加一
            Map<String,String> postMap=redisDao.queryPostSomeSumById(postId);
            if(postMap.get("commentsum")==null){
                postMap.put("commentsum",""+0);
            }
            int commentsum=Integer.parseInt(postMap.get("commentsum"));
            commentsum+=1;
            postMap.put("commentsum",""+commentsum);
            redisDao.updatePostSomeSum(postId,postMap);

            //增加1点积分
            Utils.addReward(account,1);
            Utils.toJson(a,resp);
        }catch(Exception e){
            Utils.toJson(-1,resp);
        }





    }
}
